package com.qust.wj.controller;

import com.qust.wj.constant.ResultCode;
import com.qust.wj.entity.*;
import com.qust.wj.result.Result;
import com.qust.wj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpSession;

/**
 * Created by 张靖奇 on 2020/4/26 17:36
 */
@Controller
@Slf4j
public class RegisterController {
    @Autowired
    UserService userService;
    @CrossOrigin
    @PostMapping(value = "api/register")
    @ResponseBody
    public Result register(@RequestBody User requestUser){
       // 对html标签就行转义，防止XSS攻击
        String username= requestUser.getUsername();
        String password = requestUser.getPassword();
//        username = HtmlUtils.htmlEscape(username);
        log.info(password);
        if(username.trim() == "" ||  password.trim() == "")
        { return new Result(ResultCode.PASSWORD_USERNAME_NULL); }
        User user = userService.selectByUserName(username);
        if (null == user) {
//            return new Result(400);
            userService.insertSelective(requestUser);
            return  new Result(ResultCode.SUCCESS);
        } else { return new Result(ResultCode.USER_IS_EXIST); }
    }
}
